package com.linkhub.oms.admin.common.aspect;

import cn.dev33.satoken.stp.StpUtil;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.admin.modules.user.domain.param.BaseUserParam;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Description: UserInfoFillAspect
 * @Author: echo
 * @Date: 2024/3/22 16:52
 * @Version: 1.0
 */
@Aspect
@Component
@Slf4j
@Order(0)
public class UserInfoFillAspect {

    /**
     * 以 controller 包下定义的所有请求为切入点
     */
    @Pointcut("execution(public * com.linkhub.oms.admin.modules.*.controller.*.*(..))")
    public void webAop() {
        // empty
    }

    @Around("webAop()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 开始打印请求日志
        Object[] args = proceedingJoinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof BaseUserParam && StpUtil.isLogin()) {
                BaseUserParam baseUserParam = (BaseUserParam) arg;
                baseUserParam.setUserId(LhSessionContext.getUserId());
            }
        }
        //请求结果
        return proceedingJoinPoint.proceed();
    }

}
